package com.sxr.signature.util;

import java.nio.charset.StandardCharsets;

public class UrlUtil {
    public static final String UTF_8 = StandardCharsets.UTF_8.toString();

    /**
     * URL解码 - 使用JDK自带的URLEncoder
     */
    public static String urlDecode(String value) {
        if (value == null) {
            return "";
        }
        try {
            return java.net.URLDecoder.decode(value, UTF_8);
        } catch (java.io.UnsupportedEncodingException e) {
            // UTF-8是标准编码，不会出现此异常
            throw new RuntimeException("UTF-8 encoding not supported", e);
        }
    }

    /**
     * URL编码 - 使用JDK自带的URLEncoder
     */
    public static String urlEncode(String value) {
        if (value == null) {
            return "";
        }
        try {
            return java.net.URLEncoder.encode(value, UTF_8);
        } catch (java.io.UnsupportedEncodingException e) {
            // UTF-8是标准编码，不会出现此异常
            throw new RuntimeException("UTF-8 encoding not supported", e);
        }
    }
}
